

package org.smallbun.fast.manage.activity.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.smallbun.fast.common.PageFactory;
import org.smallbun.fast.manage.activity.entity.ActivityEntity;
import org.smallbun.fast.manage.activity.entity.SignUpEntity;
import org.smallbun.fast.manage.activity.entity.UserEntity;
import org.smallbun.fast.manage.activity.service.ActivityService;
import org.smallbun.fast.manage.activity.service.SignUpService;
import org.smallbun.fast.manage.activity.service.UserService;
import org.smallbun.framework.annotation.DemoEnvironment;
import org.smallbun.framework.base.BaseController;
import org.smallbun.framework.result.AjaxResult;
import org.smallbun.framework.result.PageableResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.Model;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import static org.smallbun.framework.constant.ErrorMsgConstant.ID_NOT_BLANK_MSG;
import static org.smallbun.framework.toolkit.AutoMapperUtil.mappingList;


/**
 * @author lovec
 */
@Validated
@RestController
@RequestMapping(value = "/signup")
@Slf4j
public class SignUpController extends BaseController {

	@Autowired
	private SignUpService signUpService;

	@Autowired
	private UserService userService;

	@Autowired
	private ActivityService activityService;

	@PostMapping(value = "/add")
	public AjaxResult add(String openId,Long activityId) {
		log.info("接收到参数{}{}",openId,activityId);
		try{
			//查看是否已经报名
			Integer signUpEntity = signUpService.findSignUpByOpenIdAndActivityId(openId, activityId);
			if (signUpEntity!=0){
				return  AjaxResult.builder().status("200").msg("已经报名").build();
			}
			UserEntity user = userService.getUserByOpenId(openId);
			SignUpEntity build = SignUpEntity.builder().activityId(activityId).userId(String.valueOf(user.getId())).openId(openId).build();
			signUpService.save(build);
			return  AjaxResult.builder().status("200").msg("报名成功").build();
		}catch (Exception e){
			log.info("报名失败{}",openId);
			e.printStackTrace();
			return AjaxResult.builder().status("500").msg("报名失败").build();
		}
	}

}
